(function() {

    var vue;
    var loading;
    var loadingCount = 0; // 同时加载数量
    var UI = {
        initVue: function(vueApp) {
            vue = vueApp;
            console.log('初始化 vue 完成');
        },
        message: function(msg, type) {
            type = type || 'success';
            vue.$message({
                message: msg,
                type: type
            });
        },
        success: function(msg) {
            this.message(msg);
        },
        warning: function(msg) {
            vue.$message({
                message: msg,
                type: 'warning'
            });
        },
        error: function(msg) {
            this.message(msg, 'error');
        },
        alert: function(msg) {
            vue.$confirm(msg, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'info'
            }).then(() => {
            });
        },
        deleteConfirm: function(_confirm) {
            vue.$confirm('此操作将永久删除该项, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                _confirm();
            });
        },
        closeLoading: function() {
            loadingCount --;
            if(loadingCount == 0) {
                loading.close();	
            }
        },
        openLoading: function(text) {
            text = text || "正在执行...";
            if(loadingCount != 0) {
                $(".el-loading-text").html(text);
            }
            loadingCount ++;
            loading = vue.$loading({
                lock: true,
                text: text,
                // spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });
        },
        postc: function(url, param, _call, isDebug) {
            this.post(url, param, _call, isDebug, true);
        },
        // 带转圈的请求
        post: function(url, param, _call, isDebug, isCache) {
            if(param == null) {
                param = {};
            }
            var text = param.opts_text;
            if(text == null) {
                text = '正在加载中...'
            }
            delete param.opts_text;
            UI.openLoading(text);
            var post = $.post;
            if(isCache) {
                // console.log("带缓存的请求");
                post = $.postc;
            }
            console.log('post start', param);

            $.post(url, param, (result) => {
                console.log('post finish');
                // if(isDebug) {
                //     console.log("[ Request Start ]");
                //     console.log("url", url);
                //     console.log("param", JSON.stringify(param) );
                //     console.log("result", result);
                //     console.log("[ Request End ]");
                // }
                // var success = true;
                // if(result.status == "success") {
                //     vue.$message({
                //         message: result.msg,
                //         type: 'success'
                //     });
                // } else if(result.status == "failure") {
                //     vue.$message({
                //         message: result.msg,
                //         type: 'error'
                //     });
                //     success = false;
                //     console.error(result);
                // }
                // if(success) {
                //     _call(result);
                // } else {
                //     _call(result);
                // }
                _call(result);
                UI.closeLoading();
            }); // 默认json
        }
    };

    window.Sui = UI; // SUI
    Sui.get = UI.post;

    window.UI = UI;
}());